package com.five.community.intercepetor;

import com.five.community.bean.Admin;
import com.five.community.bean.Role;
import com.five.community.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ActivityInterceptor  implements HandlerInterceptor {

    @Autowired
    RoleService roleService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        获得登录后用户信息和角色名称
        Admin admin = (Admin) request.getSession().getAttribute("admin");

        Role role = roleService.selectById(admin.getRoleId());
//        根据角色名称进行权限认证
        if (role.getName().equals("系统管理员") || role.getName().equals("服务模块管理员")) {
            //放行
            return true;
        }else {
            //拦截
            response.sendRedirect(request.getContextPath() + "/no");
            return false;
        }


    }


}
